//
//  AppController.m
//  iTunes Playlist Selector
//
//  Created by Keith Duncan on 06/08/2006.
//  Copyright 2006 thirty-three. All rights reserved.
//

#import "AppController.h"

#import "iTunesNode.h"

#import "AFSourceList.h"

#import "AFLocateLibraryWindowController.h"

#import "iTunesLibrary.h"

@interface AppController ()
@property (retain) AFLibrary *library;
@property (readwrite, retain) AFSourceNode *rootNode;
@end

@implementation AppController

@synthesize library;
@synthesize rootNode=_rootNode;

- (void)dealloc {
	[library release];
	[_rootNode release];
	
	[super dealloc];
}

- (void)awakeFromNib {
	[playlistsController setSortDescriptors:[NSArray arrayWithObjects:
											 [[[NSSortDescriptor alloc] initWithKey:@"type" ascending:NO] autorelease],
											 [[[NSSortDescriptor alloc] initWithKey:@"position" ascending:YES] autorelease],
											 [[[NSSortDescriptor alloc] initWithKey:@"name" ascending:YES selector:@selector(caseInsensitiveCompare:)] autorelease], nil]];
	
	[sidebar bind:NSContentBinding toObject:playlistsController withKeyPath:@"arrangedObjects" options:nil];
	[sidebar bind:NSContentValuesBinding toObject:playlistsController withKeyPath:@"arrangedObjects.name" options:nil];
	[sidebar bind:AFContentImagesBinding toObject:playlistsController withKeyPath:@"arrangedObjects.image" options:nil];
	[sidebar bind:AFSelectionIndexPathBinding toObject:playlistsController withKeyPath:@"selectionIndexPath" options:nil];
}

- (IBAction)load:(id)sender {
	NSString *loadPath = [[NSUserDefaults standardUserDefaults] stringForKey:@"libraryPath"];
	if (([[NSApp currentEvent] modifierFlags] & NSAlternateKeyMask) == NSAlternateKeyMask) loadPath = AFLibraryUnknownLocation;
	
	self.library = [[[iTunesLibrary alloc] initWithFilePath:loadPath] autorelease];
	self.library.delegate = self;
	
	NSError *error = nil;
	if (![self.library loadAndReturnError:&error]) {
		if (error != nil) [NSApp presentError:error];
		
		[self.library release];
		
		return;
	}
	
	self.rootNode = [self.library rootNode];
	[libraryMenuButton setMenu:[self.library sourceMenu]];
	
	[[NSUserDefaults standardUserDefaults] setObject:self.library.path forKey:@"libraryPath"];
}

@end

@implementation AppController (Delegate)

- (BOOL)sourceList:(AFSourceList *)view canSelectObject:(iTunesNode *)node {
	if (node.type == kiTunesFolder && [[node representedObject] objectForKey:@"Playlist Items"] == nil) return NO;
	else if (node.type == kiTunesLibrary && [node.name isEqualToString:@"Podcasts"]) return NO;
	return YES;
}

- (void)requestPathForLibrary:(AFLibrary *)library {
	AFLocateLibraryWindowController *controller = [[AFLocateLibraryWindowController alloc] initWithNibName:@"KDLibraryLocator" bundle:nil];
	controller.library = self.library;
	controller.expectedLocation = self.library.path;
	
	self.library.path = [controller requestLibraryLocation];
	
	[controller release];
}

- (BOOL)library:(AFLibrary *)library isObjectSelectable:(id)object {
	return [self sourceList:nil canSelectObject:object];
}

- (NSBundle *)bundle {
	return [NSBundle bundleForClass:[self class]];
}

@end

@implementation AppController (Datasource)

- (BOOL)sourceList:(AFSourceList *)view shouldDrawDisclosureForItem:(iTunesNode *)object {
	if ([[object indexPath] length] > 1) return YES;
	if (object == [[self.rootNode childNodes] lastObject]) return YES;
	
	return NO;
}

@end
